package com.atom.module.lib.opengles.draw

import android.graphics.PointF
import android.opengl.GLES30
import android.util.Log
import com.atom.module.lib.opengles.ext.scale
import com.atom.module.lib.opengles.ext.translate
import com.atom.module.lib.opengles.util.BufferUtils
import com.atom.module.lib.opengles.util.ShaderUtils
import com.blankj.utilcode.util.LogUtils
import java.nio.FloatBuffer
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin

@Suppress("unused")
class GlLine(points: List<PointF> = arrayListOf()) : Gl2dDrawable() {

    override var vertexArray: FloatBuffer = BufferUtils.floatBuffer(4)

    override var drawMode: Int = GLES30.GL_LINES

    fun setPoints(points: List<PointF>) {
        if (vertexArray.capacity() < points.size * 2) {
            vertexArray = BufferUtils.floatBuffer(points.size * 2)
        } else {
            vertexArray.clear()
        }
        points.forEach {
            vertexArray.put(it.x)
            vertexArray.put(it.y)
        }
        vertexArray.flip()
        notifyVertexArrayChange()
    }

    init {
        if (points.size >= 2) {
            setPoints(points)
        }
    }

    override fun draw() {
        LogUtils.e(drawMode, vertexCount)
        GLES30.glDrawArrays(drawMode, 0, vertexCount)
        ShaderUtils.checkGLError("glDrawArrays")
    }
}